package com.yinp.proapp.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.yinp.proapp.R;
import com.yinp.proapp.adapter.ComViewHolder;
import com.yinp.proapp.adapter.CommonAdapter;
import com.yinp.proapp.adapter.SingleViewHolder;
import com.yinp.proapp.databinding.ItemPopupBinding;

import java.util.Arrays;
import java.util.List;

/**
 * @author :yinpeng
 * date      :2021/11/25
 * package   :com.yinp.proapp.utils
 * describe  :
 */
public class PopupWindowCommon {
    public interface OnClickListener {
        void clickSure(int position);
    }

    public static PopupWindow initPopupWindow(Context context, OnClickListener listener, String[] text) {
        PopupWindow popupWindow = new PopupWindow(context);
        popupWindow.setWidth((int) AppUtils.dpToPx(110));
        popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        @SuppressLint("InflateParams") View view = LayoutInflater.from(context).inflate(R.layout.poup_select, null);
        popupWindow.setContentView(view);
        popupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));//view中有圆角的话需要用到这个，这只透明
        popupWindow.setOutsideTouchable(false);
        popupWindow.setFocusable(true);

        List<String> listData = Arrays.asList(text);
        RecyclerView recyclerView = view.findViewById(R.id.pop_recycle);

        CommonAdapter<String> adapter = new CommonAdapter<String>(context, listData) {
            @Override
            protected SingleViewHolder setComViewHolder(View view, int viewType, ViewGroup parent) {
                return new SingleViewHolder(ItemPopupBinding.inflate(mInflater, parent, false));
            }

            @Override
            public void onBindItem(SingleViewHolder holder, int position, String item) {
                ItemPopupBinding binding = (ItemPopupBinding) holder.getBinding();
                if (listData.size() - position == 1) {
                    holder.itemView.setVisibility(View.GONE);
                }
                binding.itemTitle.setText(item);
            }
        };
        LinearLayoutManager layoutManager = new LinearLayoutManager(context) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener((position, view1) -> {
            if (listener != null) {
                listener.clickSure(position);
            }
        });
        return popupWindow;
    }

    static class ViewHolder extends ComViewHolder {
        ItemPopupBinding bind;

        ViewHolder(ItemPopupBinding view) {
            super(view.getRoot());
            bind = view;
        }
    }

}
